import java.util.ArrayList;


public class AlertTree implements tree {
	
	private Alert node;
	private Alert parent;
	private ArrayList<AlertTree> children; 
	
	
	
	/**
	 * Constructor for root of tree
	 * 
	 * @param alert
	 */
	public AlertTree(Alert alert){
		
		node = alert;
		this.children = new ArrayList<AlertTree>();
		parent = null;
	}
	
	
	/**
	 * Constructor needed to create child nodes
	 * 
	 * @param alert
	 * @param parent
	 */
	private AlertTree(Alert alert, Alert parent){
		
		node = alert;
		this.children = new ArrayList<AlertTree>();
		this.parent = parent;
		
	}	
	
	
	/** 
	 * Add new alert to current alert. 
	 * The new alert will be a child node to the current node
	 * 
	 * @Override
	 */
	public void addPostcondition(Alert newAlert) {
		// TODO Auto-generated method stub
		
		//if (this.children.size() == 0){
			//this.children = new ArrayList<AlertTree>();
			this.children.add(new AlertTree(newAlert, this.getNode()));
			
		//} else{
		//	this.children.add(new AlertTree(alert));
			
		//}		
	}
	
	/**
	 * Goes through the whole tree and adds childnodes to each alert
	 * that has the same name as in the parentAlerts arraylist 
	 * 
	 * @param newAlert
	 * @param parentAlerts
	 */
	public void addMultPostConditionTo(Alert newAlert, ArrayList<String> parentAlerts) {
		
		//System.out.println(parentAlerts.toString());
		//System.out.println("Present node" + this.node.getAlert_type());
		//checks if the alert name is in the possible parent list
		//if so, adds the alert as a child
		
		
		for (String parentA : parentAlerts){
		
			
			if(this.node.getAlert_type().contains(parentA))//parentAlerts.contains(this.node.getAlert_type())){
			//if(parentAlerts.contains(this.node.getAlert_type()))
			{
				this.children.add(new AlertTree(newAlert, this.getNode()));
				
			} else{
				
				if(this.children.size() != 0){
					
					for(int i=0; i < this.children.size(); i++){
						
						children.get(i).addMultPostConditionTo(newAlert, parentAlerts);					
					}
				}
			}
		}
	}


	@Override
	public Alert getNode() {
		// TODO Auto-generated method stub
		return node;
	}
	
	

	/**
	 * Add new alert to specific node
	 * The new alert will be a child node to the specific node
	 * 
	 * @Override
	 */
	public void addPostConditionTo(Alert newAlert, String specificalert_type) {
		
		
		if(this.node.getAlert_type().contains(specificalert_type)){
				//.equalsIgnoreCase(specificalert_type)){
			
			this.children.add(new AlertTree(newAlert, this.getNode()));
			
		} else{
			
			if(this.children.size() != 0){
				
				for(int i=0; i < this.children.size(); i++){
					
					children.get(i).addPostConditionTo(newAlert, specificalert_type);					
				}
			}
		}
	}

	

	@Override
	public Alert getParent() {
		// TODO Auto-generated method stub
		return this.parent;
	}
	
	
	/**
	 * Print whole alert tree in GraphViz format
	 * 
	 * @Override
	 */
	public void toDot() {
		// TODO Auto-generated method stub
		
		if (this.parent != null){
			
			if(this.children != null){
				for(int i=0; i < this.children.size(); i++){
					System.out.println("\"" + this.getNode().getAlert_type() + "\"->\""+ this.children.get(i).getNode().getAlert_type() + "\""); //need to close bracket
					this.children.get(i).toDot();
				}
			}
			
		}else{
			
			//System.out.println("digraph G { ");
			
			if(this.children != null){
				for(int i=0; i < this.children.size(); i++){
					System.out.println("\"" + this.getNode().getAlert_type() + "\"->\""+ this.children.get(i).getNode().getAlert_type() + "\"") ; //need to close bracket
					this.children.get(i).toDot();
				}
			}
		}		
	}
	

	@Override
	public ArrayList<AlertTree> getChildren() {
		// TODO Auto-generated method stub
		return this.children;
	}

}
